package com.MTR;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
//import java.util.Random;

import javax.imageio.*;
import javax.swing.*;
//import javax.swing.border.*;
import javax.swing.event.*;


//TODO add train configuration UI


public class MainUI {

	// Tab String
    final static String MONITORPANEL = "Monitoring";
    final static String HISTORYPANEL = "History";
    final static String HEALTHPANEL = "Health Check";
    final static String SETTINGPANEL = "Setting";
    
    final static int extraWindowWidth = 100;

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("MTR Temperature Monitor System");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JTabbedPane tabbedPane = new JTabbedPane();

        //Create the "cards".
        final TrainPanel card1 = new TrainPanel();
        final HistoryPanel card2 = new HistoryPanel();
        final HealthCheckPanel card3 = new HealthCheckPanel();
        final SettingPanel card4 = new SettingPanel();

        tabbedPane.addTab(MONITORPANEL, card1);
        tabbedPane.addTab(HISTORYPANEL, card2);
        tabbedPane.addTab(HEALTHPANEL, card3);
        tabbedPane.addTab(SETTINGPANEL, card4);
        
        tabbedPane.addChangeListener(new ChangeListener(){ 
            public void stateChanged(ChangeEvent e){ 
            	JTabbedPane tabbedPane = (JTabbedPane)e.getSource(); 
            	int selectedIndex = tabbedPane.getSelectedIndex(); 
            	if(selectedIndex == 1){ 
            		//card2.updateChart();
            	}
            }
        });
        // add top Image
        JPanel pTop = new JPanel();
        
        Image myPicture = null;
		try {
			File img = new File("images/logo.jpg");
			myPicture = ImageIO.read(img).getScaledInstance(150, 50, Image.SCALE_DEFAULT);
		} catch (IOException e) {
			e.printStackTrace();
		}
        final JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
        pTop.add( picLabel );
        
        // Add Top Start / Stop Button
        final JButton st = new JButton("Start");
        final JButton ed = new JButton("End");
        
        st.addActionListener(new ActionListener() {
		    	public void actionPerformed(ActionEvent arg0) 
		    	{
		    		// start com port
		    		if (!DataReceiver.instance().start(17))
		    		{
		    			JOptionPane.showMessageDialog(st, "Can NOT open device. Please check your USB port.");
		    			return ;
		    		}
		    		// update UI
		    		st.setEnabled(false);
		    		ed.setEnabled(true);
		    	}
        });

        ed.addActionListener(new ActionListener() {
		    	public void actionPerformed(ActionEvent arg0) 
		    	{
		    		// change the logic in case user click too frequent.
		    		ed.setEnabled(false);
		    		DataReceiver.instance().stop();
		    		st.setEnabled(true);
		    	}
        });

        
        ed.setEnabled(false);
        pTop.add(st);
        pTop.add(ed);
        
        frame.add(pTop, BorderLayout.NORTH);
        
        // add bottom
        frame.add(tabbedPane, BorderLayout.CENTER);

        //Display the window.
        frame.setSize(1024, 768);
        frame.setVisible(true);
        
    }

    public static void main(String[] args) {

        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }


}
